package com.zhangtai.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.Neusoft.constVo.EmployeeInfoConst;
//import com.zhangtai.Neusoft.service.NeuEmployeeBaseInfoService;
import com.zhangtai.modules.dao.EmployeeInfoDao;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.vo.MailVo;
import com.zhangtai.utils.EmailUtil;
import com.zhangtai.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class AsyncEmployeeMethod {

    @Autowired
    private EmployeeInfoDao employeeInfoDao;

    @Autowired
    private EmailUtil emailUtil;
//    @Autowired
//    private NeuEmployeeBaseInfoService neuEmployeeBaseInfoService;

    @Async("Log")
    public void sendEmail(String mail, Long oid, String signUrl,String title) throws Exception {
        if (!org.springframework.util.StringUtils.isEmpty(mail)){
            MailVo mailVo = new MailVo();
            mailVo.setOid(oid);
            mailVo.setSubject(title);
            mailVo.setReceive(mail);
            mailVo.setContent("点击前往："+signUrl);
            mailVo.setType(1); //0:offer,1:签约邮件

            sendMail(mailVo);

        }
    }

    /**
     * 异步保存用户信息
     * @param employeeInfoEntity
     */
    @Async("Log")
    @Transactional
    public void saveEhrEmployee(List<EmployeeInfoEntity> employeeInfoEntity){
        List<EmployeeInfoEntity> employeeInfoEntityList=new ArrayList<>();
        for(EmployeeInfoEntity entity:employeeInfoEntity){
            EmployeeInfoEntity oldInfo = employeeInfoDao.selectOne(new QueryWrapper<EmployeeInfoEntity>().eq("idcard",entity.getIdcard()));
            if(ObjectUtils.isEmpty(oldInfo)){
//                如果职级为空，则设置进去
                Long jobLevelId = entity.getJobLevelId();
                if (ObjectUtils.isEmpty(jobLevelId)){
                    setJobLevel(entity);
                }

                entity.setEmployeeOid(Long.parseLong(getUserCode()));
                entity.setInsetDate(new Date());
                employeeInfoDao.insert(entity);
                employeeInfoEntityList.add(entity);
            }
        }
//        neuEmployeeBaseInfoService.sendMq(employeeInfoEntityList, EmployeeInfoConst.EMPLOYEE_INFO_SCHEDULE,"employeeInfoEntityList",true);
    }

    private void setJobLevel(EmployeeInfoEntity entity) {
        Long professionJoblevelId = entity.getProfessionJoblevelId();
        Long managementJoblevelId = entity.getManagementJoblevelId();
        if (managementJoblevelId!=null){
            entity.setJobLevelType(2); //1:管理。2：专业
            entity.setJobLevelId(managementJoblevelId);
        }else {
            entity.setJobLevelType(1);
            entity.setJobLevelId(professionJoblevelId);
        }
    }

    public String getUserCode(){
        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String datestr = format.format(date);
        String nm = new SimpleDateFormat("yyyyMMdd").format(date);
        Long max = employeeInfoDao.getEmployeeOidList(datestr)+1;
        String employeeOidStr = String.format("%05d", max);
        return nm+employeeOidStr;
    }

    public R sendMail(@RequestBody @Valid MailVo mailVo) throws Exception {
        String content = mailVo.getContent();
        String receive = mailVo.getReceive();
        String subject = mailVo.getSubject();


        String text=null;
        if (StringUtils.isEmpty(receive )){
            return R.error("收件人不能为空");
        }
        if (StringUtils.isEmpty(subject)){
            return R.error("主题不能为空");
        }
        if (StringUtils.isEmpty(content)){
            text=subject;

        }else {
            text=content;
        }
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();





        List<String> urls = mailVo.getUrl();
        if (CollectionUtils.isEmpty(urls)){
            //            不带附件
            emailUtil.sendHtml(receive ,subject,text);


        }else {
            //            带附件
            emailUtil.sendHtmlAttachmentList(receive,subject,content,urls);  //发送带附件邮件


        }
        return R.ok();
    }
}
